<template>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
        <el-form :inline="true" class="demo-form-inline" :disabled="disabled">

            <el-form-item label="一级分类">
                <el-select placeholder="请选择" v-model="category1Id" @change="getCategory2List">
                    <el-option 
                        v-for="item in catecoryList1"
                        :key="item.id"
                        :label="item.name" 
                        :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="二级分类">
                <el-select placeholder="请选择" v-model="category2Id" @change="getCategory3List">
                    <el-option 
                        v-for="item in catecoryList2"
                        :key="item.id"
                        :label="item.name" 
                        :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="三级分类">
                <el-select placeholder="请选择" v-model="category3Id">
                    <el-option 
                        v-for="item in catecoryList3"
                        :key="item.id"
                        :label="item.name" 
                        :value="item.id" />
                </el-select>
            </el-form-item>

        </el-form>
    </el-card>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { storeToRefs } from "pinia";

export default defineComponent({
    name: "Category",
});
</script>

<script setup lang="ts">
import { onMounted } from "vue";
import useAttrStore from "@/stores/attr"
const attrStore = useAttrStore()

//props接收商品属性列表传递的布尔值控制三级列表是否禁用
defineProps<{
    disabled: boolean
}>()

//解构出仓库中的数据
const {
    catecoryList1 , category1Id , 
    catecoryList2 , category2Id , 
    catecoryList3 , category3Id
} = storeToRefs(attrStore)

//1.初始化请求一级列表
onMounted(()=>{
    attrStore.getCategory1()
})

//2.改变一级列表的回调
const getCategory2List = () => {
    //点击一级之前，先清空二级和三级数据
    catecoryList2.value = []
    category2Id.value = null
    catecoryList3.value = []
    category3Id.value = null
    //请求二级列表
    attrStore.getCategory2()
}

//2.改变二级列表的回调
const getCategory3List = () => {
    //点击一级之前，先清空三级数据
    catecoryList3.value = []
    category3Id.value = null
    //请求三级列表
    attrStore.getCategory3()
}
</script>